<?
	if (!defined('i-Teck_ADMIN')) die("Hacking attempt");

	switch($mod){
		case "add":
			
		?>
<form name="form1" method="post" action="">
		<article class="module width_full">
			<header><h3>Thêm Danh Mục Phụ Kiện</h3></header>
				<div class="module_content">	
				<fieldset>
							<label>Tên Danh Mục</label>
						    <input name="name" type="text" id="name" size="50">
				</fieldset>
				<fieldset>
							<label>Ảnh (90 x 30px)</label>
				</fieldset>
					<fieldset>
							<input name="img" type="text" id="img" size="50"/>
							<input type="button" value="Upload" onClick="window.open('?act=danhmucphukien&mod=upload','test1','width=450,height=380');">
					</fieldset>
				
				<input type="submit" name="cmd" id="cmd" value="Thêm" class="alt_btn">
				</div>
		</article><!-- end of styles article -->
</form>
<?
if(isset($_REQUEST["cmd"])=="Thêm"){
	mysql_query("insert into shop_danhmucphukien (name, img) 
				values ('".addslashes($_POST["name"])."','".addslashes($_POST["img"])."')");
	mysql_close();
	echo "<meta http-equiv='refresh' content='0; url=?act=danhmucphukien&mod=view'>";
	}
	break;
	case "view":
	$result=mysql_query("SELECT * FROM shop_danhmucphukien ORDER BY STT");
	if(mysql_num_rows($result)<=0){echo"<center><font color=red>Chưa có dữ liệu</font></center>";}
	else{
	?>

<form id="form2" name="form2" method="post" action="">

	<article class="module width_full">
			<header><h3>Quản lý Danh Mục</h3></header>
	
		<div class="tab_container">
			<div id="tab1" class="tab_content">
			<table class="tablesorter" cellspacing="0"> 
			<thead> 
				<tr> 
    				<th>STT</th> 
    				<th>Tên Danh Mục</th> 
    				<th>Sửa</th> 
    				<th>Xóa</th>
				</tr> 
			</thead> 
			<tbody> 


    <?  while($r=mysql_fetch_array($result)){?>
				<tr> 
   					<td><input name="stt[]" type="text" value="<?=$r['stt'];?>" size="2" /><input name="id[]" type="text" value="<?=$r['id'];?>" style="display:none" /></td> 
    				<td><a href="?act=danhmucphukien&amp;mod=edit&amp;id=<? echo $r['id'];?>"><? echo stripslashes($r['name']);?></a></td> 
    				<td><a href="?act=danhmucphukien&amp;mod=edit&amp;id=<? echo $r['id'];?>">Sửa</a></td> 
    				<td><a onclick="return  confirm('Bạn có muốn xóa Danh Mục này ko?');" href="?act=danhmucphukien&amp;mod=delete&amp;id=<? echo $r['id'];?>">Xóa</a></td> 
				</tr> 
    
    <? }}?>
    <tr>
      <td><input type="submit" name="cmd" id="cmd" value="Sửa thứ tự" class="alt_btn"/></td>
    </tr>

			</tbody> 
			</table></div></div>
		</article><!-- end of styles article -->
</form>

<? 
if(isset($_POST["cmd"])=="Sửa thứ tự"){
		$id=$_POST["id"];
		$stt=$_POST["stt"];
		$lap=count($_POST["id"]);
		for($i=0;$i<$lap;$i++){
		mysql_query("UPDATE shop_danhmucphukien SET stt=".$stt[$i]." WHERE id=".$id[$i]."");

			}
	
		echo "<meta http-equiv='refresh' content='0; url=?act=danhmucphukien&mod=view'>";
	}

break;
		case "edit":
		$id=intval($_REQUEST["id"]);
		$result=mysql_query("select * from shop_danhmucphukien where id=$id");
		if(mysql_num_rows($result)<=0){echo"<center><font color=red>Chưa có dữ liệu</font</center>";}
		else{
		?>
        <form name="form1" method="post" action="">
	<article class="module width_full">
			<header><h3>Sửa Danh Mục</h3></header>
				<div class="module_content">	
				<fieldset>
							<label>Tên Danh Mục</label>
						    <? while($r=mysql_fetch_array($result)){ ?>
							<input name="name" type="text" value="<? echo stripslashes($r["name"]);?>" size="50">
				</fieldset>
				<fieldset>
							<label>Ảnh (90 x 30px)</label>
				</fieldset>
					<fieldset>
							<input name="img" type="text" id="img" size="50"  value="<?=$r['img']?>"/><? }?>
							<input type="button" value="Upload" onClick="window.open('?act=danhmucphukien&mod=upload','test1','width=450,height=380');">
					</fieldset>
				
				<input type="submit" name="cmd" id="cmd" value="Sửa" class="alt_btn">
				</div>
		</article><!-- end of styles article -->
</form>
		
	<? } if(isset($_REQUEST["cmd"])=="Sửa"){
		mysql_query("update shop_danhmucphukien set name='".addslashes($_POST["name"])."' where id=$id");
		mysql_query("update shop_danhmucphukien set img='".addslashes($_POST["img"])."' where id=$id");
		mysql_close();
		echo "<meta http-equiv='refresh' content='0; url=?act=danhmucphukien&mod=view'>"; }
	
	break;
	case "delete":
		$id=intval($_REQUEST["id"]);
		$re=mysql_query("SELECT * FROM shop_danhmucphukien where id=$id");
		while($r=mysql_fetch_array($re))
		{
			unlink('../img'.$r["img"]);
		}
		mysql_query("delete from shop_danhmucphukien where id=$id");
		mysql_close();
		echo "<meta http-equiv='refresh' content='0; url=?act=danhmucphukien&mod=view'>";
break;

case "upload":
	
?>





<form enctype="multipart/form-data" method="post">
  <div align="center">
    <h1>Upload file 
    </h1>
  </div>
  
  
  <table width="400" border="0" align="center" cellpadding="3" cellspacing="3">
    <tr>
      <td>Chọn file: </td>
      <td><input name="f1" type="file" id="f1"></td>
    </tr>
    
    
    <tr>
      <td>&nbsp;</td>
      <td><input name="cmd" type="submit" id="cmd" value="Upload">
        <input type="reset" name="Reset" value="Reset"></td>
    </tr>
  </table>
</form>
<?

	if ($_REQUEST["cmd"] =="Upload"){
		$a = $_FILES["f1"]["tmp_name"];
		$b = $_FILES["f1"]["name"];
		$c = $_FILES["f1"]["size"];
		$d = $_FILES["f1"]["type"];
		$e = $_FILES["f1"]["error"];
		
		
		echo strstr("image",$d); 
		if (!getimagesize($_FILES['f1']['tmp_name']))
			{ echo "<font color=red><center>Invalid Image File...</center></font>";
			exit();
			}
			
			if (substr($d,0,5)=="image"){
				
		move_uploaded_file($a,"../img/danhmucphukien/".$b);
		echo "Đã upload thành công file <b>{$b}</b>!<br>Click vào ảnh để chọn file này.<br>";
		
		echo "<a title='Chọn file này' href=# onclick=\"window.opener.document.form1.img.value='$web_urlimg/danhmucphukien/$b';window.close();\"><img border=0 width=80 src=../img/danhmucphukien/".$b." height=60></a>";
		

		
		
		} else {
		echo "Upload không thành công!";
		}
	}
			break;



	} 
?>
  
  
  
  
  
  
